
import org.cgfy.generator.Driver;
import org.cgfy.generator.constant.DM;
import org.cgfy.generator.constant.MySQL;
import org.cgfy.generator.constant.Oracle;
import org.cgfy.generator.models.ClassContent;
import org.cgfy.generator.models.Template;
import org.cgfy.generator.options.Options;
import org.cgfy.generator.options.DatabaseOptions;
import java.util.HashMap;
import java.util.Map;
public class Executor {
    //达梦
    public static final String jdbcUrl ="jdbc:dm://localhost:5236/KJGW";
    public static final String jdbcUser = "KJGW";
    //public static final String jdbcPassword = "SYSDBA";
    public static final String jdbcPassword = "3DV9saG*x2cc7A";
    public static final String databaseType = "dm";
    //MySQL
/*    public static final String jdbcUrl ="jdbc:mysql://localhost:3306/kjgw";
    public static final String jdbcUser = "root";
    public static final String jdbcPassword = "123456";
    public static final String databaseType = "mysql";*/

    public static final String tableName =  "CMS_CONTENT";
    public static final String javaFileName =  "CmsContent";
    public static final String packageUrl = "cms";
    public static void main(String[] args){
        try {
            String prePackageUrl="org.techwebsite.";//前缀包路径(不变)
            String javaTemplatePath="cms";//(不变)
            Map<String, Template> templates = new HashMap<String, Template>();
            String outputPath=System.getProperty("user.dir");
            System.setProperty("user.dir",System.getProperty("user.dir")+"/templates");
            //java生成
            templates.put("entity", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("EntityLombok.ftl")//Lombok模板为EntityLombok.ftl
                    .setPackageName(prePackageUrl+"domain."+packageUrl)
                    .setClassName(javaFileName+"Dto"));
            templates.put("dao", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Dao.ftl")
                    .setPackageName(prePackageUrl+"logic.mapper."+packageUrl)
                    .setClassName(javaFileName + "Mapper"));
            templates.put("mapper", new Template()
                    .setPath(javaTemplatePath)
                    .setFile("Mapper.ftl")
                    .setPackageName("mapper."+packageUrl)
                    .setSourceFolder("src/main/resources")
                    .setClassName(javaFileName + "Mapper")
                    .setSuffix(".xml")
            );


            DatabaseOptions databaseOptions=null;
            // 数据库连接信息
            if("mysql".equals(databaseType)){
                databaseOptions = new DatabaseOptions(MySQL.MYSQL_8_DERVER, jdbcUrl,jdbcUser,jdbcPassword);
            }else if ("oracle".equals(databaseType)){
                databaseOptions = new DatabaseOptions(Oracle.ORACLE_DERVER, jdbcUrl);
                databaseOptions.setUser(jdbcUser);
                databaseOptions.setPassword(jdbcPassword);
            }else if ("dm".equals(databaseType)){
                databaseOptions = new DatabaseOptions(DM.DM_DERVER, jdbcUrl);
                databaseOptions.setUser(jdbcUser);
                databaseOptions.setPassword(jdbcPassword);
            }
            // 初始化执行工具
            Driver driver = Driver.instance(new Options(databaseOptions).setOutputPath(outputPath+"/GENERATE/"));
            // 读取表信息
            ClassContent content = driver.readTable(tableName);
            // 创建代码
            driver.createCode(content,templates);
            System.out.println("生成成功!");
        }catch (java.lang.Exception e){
            System.out.println("生成失败,错误信息:"+e.getMessage());
            e.printStackTrace();
        }

    }
}
